home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / russell / russell.lha / examples / tree.r < prev    next >
Text File  |  1989-12-29  |  1KB  |  32 lines

  1. (* Binary tree data type.  Uses a functional representation for tree *)
  2. (* nodes.                                                            *)
  3. func [L: type {}] {
  4.   let
  5.     lr == enum { left, right };
  6.   in use lr in
  7.     union B { leaf: val L; interior: func [val lr] val B }
  8.     with B {
  9.              left_sub_tree == func [x: val B] {
  10.                                 B$to_interior[x][left]
  11.                               };
  12.              right_sub_tree == func [x: val B] {
  13.                                  B$to_interior[x][right]
  14.                                };
  15.              leaf_value == B$to_leaf;
  16.              make_leaf == B$from_leaf;
  17.              make_tree == func [l,r: val B] val B {
  18.                             B$from_interior [
  19.                                 func [x: val lr] {
  20.                                     if
  21.                                         x = left ==> l
  22.                                     #   x = right ==> r
  23.                                     fi
  24.                                 }
  25.                             ]
  26.                           }
  27.     }
  28.     export { New; :=; V; left_sub_tree; right_sub_tree; leaf_value;
  29.              make_leaf; is_leaf; make_tree }
  30.   ni ni
  31. }
  32.